<html><body style="margin: 0px; padding: 0px; font-size: 90%; font-family: trebuchet ms,arial,helvetica;"><?  
chdir('../../');
include('lib/genlib.php');
include('lib/cq-io.php');
include('etc/config.php');

$p = $_SESSION['upl-'.$_REQUEST['ctx']];
$target_dir = getDefault($p['dir'], 'resource/');

$ctxdirSeg = explode('/', $target_dir.$_REQUEST['ctx']);
unset($ctxdirSeg[sizeof($ctxdirSeg)-1]);
$ctxdir = implode('/', $ctxdirSeg).'/';

function sanitize_filename($file)
{
  $result = '';
  $file = strtolower($file);
  for($a = 0; $a < strlen($file); $a++)
  {
    $c = ord(substr($file, $a, 1));
    if ($c >= ord('a') && $c <= ord('z')) $result .= chr($c); 
    else if ($c >= ord('0') && $c <= ord('9')) $result .= chr($c);
    else if ($c == ord('.')) $result .= chr($c);
    else $result .= '-';
  }
  return($result);
}

if ($_REQUEST['ctx'] == '')
{
  logError('error calling file-upload component');
}
else
{
  if ($_REQUEST['cmd'] == 'delete')
  {
    unlink($ctxdir.$_REQUEST['f']);
  }
?><form style="background: #e0e0e0;" id="fo_form" method="post" action="?" enctype="multipart/form-data">
  &nbsp;upload new file: <input type="file" size="20" name="btn_upload" onchange="
    document.getElementById('fo_form').style.visibility = 'hidden';
    document.getElementById('fo_form').submit();"/>
  <input type="hidden" name="ctx" value="<?= htmlentities($_REQUEST['ctx']) ?>"/>
</form>
<?
  if (sizeof($_FILES)>0)
  {
    foreach ($_FILES as $file)
    {
      $ext = sanitize_filename($file['name']);
      $fname = $_REQUEST['ctx'].'-'.substr(md5(time()), 24).'-'.$ext;
      if (move_uploaded_file($file['tmp_name'], $target_dir.$fname))
      {
        chmod($target_dir.$fname, 0777);
      }
    }
  }

?><div style="font-size: 90%; font-family: consolas, monospace;"><?

  $fileList = file_list($ctxdir);
  foreach ($fileList as $fle)
  {
    ?><div><a href="?ctx=<?= urlencode($_REQUEST['ctx']) ?>&f=<?=
      urlencode($fle) ?>&cmd=delete"><img border="0" align="absmiddle" src="../../lib/img/delete.png"/></a>
      <?= $fle ?></div><?
  }
  
?></div><?

}
?></body></html>
